<template>
  <div class="login-container">
    <div style="width: 400px;height: 380px;margin:150px auto;background-color: rgba(107,149,224,0.5);border-radius: 10px">
      <div style="width: 100%;height: 100px;font-size: 30px;line-height: 60px;text-align: center;color: #4a5ed0">欢迎登录高中生<br>考试压力测评系统</div>
      <div style="margin-top: 25px;text-align: center;height: 320px">
        <el-form :model="admin">
          <el-form-item>
            <el-input v-model="admin.name" prefix-icon="el-icon-user" style="width: 80%" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="admin.password" prefix-icon="el-icon-user" show-password style="width: 80%" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item>
            <div style="display: flex;justify-content: center">
              <el-input v-model="admin.verCode" prefix-icon="el-icon-user" style="width:42%;margin-right: 10px" placeholder="请输入验证码"></el-input>
              <img :src="captchaUrl" @click="clickImg()" width="140px" height="33px"/>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 40%;margin-top: 20px" type="primary" @click="turnreg()">注册</el-button>
            <el-button style="width: 40%;margin-top: 10px" type="primary" @click="login()">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>

import request from "@/utils/request";

export default {
  name:"Login",
  data() {
    return {
      admin:{},
      key:'',
      captchaUrl:'',
    }
  },
  created() {
    this.key=Math.random();
    this.captchaUrl='http://localhost:8080/zcy/captcha?key='+this.key
  },
  methods:{
    clickImg(){
      this.admin.verCode='';
      this.key=Math.random();
      this.captchaUrl='http://localhost:8080/zcy/captcha?key='+this.key
    },
    turnreg(){
      this.$router.push("/register")
    },
    login(){
      request.post("/admin/login?key="+this.key,this.admin).then(res=>{
        if(res.code==='0'){
          this.$message({
            message: '登录成功',
            type: 'success'
          });
          localStorage.setItem("user",JSON.stringify(res.data));
          this.$router.push("/home");
        }else{
          this.$message.error({
            message: res.msg,
            type: 'error'
          })
          this.admin.verCode='';
          this.key=Math.random();
          this.captchaUrl='http://localhost:8080/zcy/captcha?key='+this.key
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container{
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw; /* 100% of viewport width */
  height: 100vh; /* 100% of viewport height */
  overflow: hidden;
  background-image: url("@/assets/封面.jpg");
  background-size: cover;
  display: flex;
  align-content: center;
  justify-content: center;
}
</style>